package com.meituan.catering.management.product.biz.service.impl;

import com.meituan.catering.management.common.model.biz.UserContextBO;
import com.meituan.catering.management.product.api.http.model.request.CreateProductHttpRequest;
import com.meituan.catering.management.product.api.http.model.request.SaveProductHttpRequest;
import com.meituan.catering.management.product.api.http.model.request.UpdateProductHttpRequest;
import com.meituan.catering.management.product.biz.model.ProductBO;
import com.meituan.catering.management.product.biz.model.converter.ProductBOConverter;
import com.meituan.catering.management.product.biz.service.ProductAccessoryBizService;
import com.meituan.catering.management.product.dao.mapper.ProductAccessoryMapper;
import com.meituan.catering.management.product.dao.model.ProductAccessoryDO;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 加料逻辑
 */
@Service
public class ProductAccessoryBizServiceImpl implements ProductAccessoryBizService {

    @Resource
    ProductAccessoryMapper productAccessoryMapper;


    @Override
    public List<ProductBO.AccessoryGroup> findAllByProductId(Long tenantId, Long productId) {
        List<ProductAccessoryDO> allByProductId = productAccessoryMapper.findAllByProductId(tenantId, productId);
        List<ProductBO.AccessoryGroup> accessoryGroups = this.groupAcc(allByProductId);
        return accessoryGroups;
    }

    @Override
    public int insetList(UserContextBO userContextBO, List<CreateProductHttpRequest.AccessoryGroup> accessoryReq, Long productId) {
        List<ProductAccessoryDO> accessoryDOS = this.buildProductAccessoryDO(userContextBO, accessoryReq, productId);

        /**
         * 批量插入
         */
        int insert=productAccessoryMapper.insertList(accessoryDOS);

        return insert;
    }

    @Override
    public List<Long> updataList(UserContextBO userContextBO, List<UpdateProductHttpRequest.AccessoryGroup> accessoryReq, Long productId) {
        List<ProductAccessoryDO> accessoryDOS = this.buildProductAccessoryDO(userContextBO, accessoryReq, productId);
        List<Long> ids=new ArrayList<>();
        for (ProductAccessoryDO proDO:accessoryDOS) {
            int insert = productAccessoryMapper.insert(proDO);
            ids.add(proDO.getId());
        }
        return ids;
    }


    @Override
    public Integer delete(UserContextBO userContextBO, Long productId) {
        return productAccessoryMapper.deleteByProductId(userContextBO.getTenantId(),productId);
    }
    /**
     * 加料组集合转DO
     * @param userContextBO
     * @param requests
     * @param productId
     * @return
     */
    private List<ProductAccessoryDO> buildProductAccessoryDO(UserContextBO userContextBO, List<CreateProductHttpRequest.AccessoryGroup> requests, Long productId)
    {
        List<ProductAccessoryDO> accessoryDOS=new ArrayList<>();

        for (CreateProductHttpRequest.AccessoryGroup accG:requests) {

            for (SaveProductHttpRequest.AccessoryGroup.Option accOption: accG.getOptions()) {
                ProductAccessoryDO productAccessoryDO=new ProductAccessoryDO();
                productAccessoryDO.setGroupName(accG.getName());
                productAccessoryDO.setTenantId(userContextBO.getTenantId());
                productAccessoryDO.setProductId(productId);
                productAccessoryDO.setUnitPrice(accOption.getUnitPrice());
                productAccessoryDO.setUnitOfMeasure(accOption.getUnitOfMeasure());
                productAccessoryDO.setName(accOption.getName());
                accessoryDOS.add(productAccessoryDO);
            }


        }
        return accessoryDOS;
    }

    /**
     * 加料组集合
     * 1.根据加料组组名 分组转BO
     * 2.遍历分好组的map k为组名 v为加料集合 转换为List Acc BO
     */
    private  List<ProductBO.AccessoryGroup> groupAcc(List<ProductAccessoryDO> productAccessoryDOS){
        Map<String, List<ProductAccessoryDO>> collect = productAccessoryDOS.stream().collect(Collectors.groupingBy(ProductAccessoryDO::getGroupName));
        List<ProductBO.AccessoryGroup> accessoryGroups=new ArrayList<>();
        collect.forEach((k,v)->{
            ProductBO.AccessoryGroup accessoryGroup=new ProductBO.AccessoryGroup();
            accessoryGroup.setName(k);
            accessoryGroup.getOptions().addAll(v.stream().map(ProductBOConverter::fromDOToAccOption).collect(Collectors.toList()));
            accessoryGroups.add(accessoryGroup);
        });
        return accessoryGroups;
    }

}
